package com.pig4cloud.mall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.mall.product.entity.Category;
import com.pig4cloud.mall.product.service.CategoryService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商品三级分类
 *
 * @author fangyu
 * @date 2020-11-25 15:05:09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
@Api(value = "category", tags = "商品三级分类管理")
public class CategoryController {

    private final CategoryService categoryService;

	/**
	 * 返回树形分类集合
	 * @param lazy 是否是懒加载
	 * @param parentId 父节点ID
	 * @return 树形菜单
	 */
	@GetMapping(value = "/tree")
	public R getTree(boolean lazy, Integer parentId) {
		return R.ok(categoryService.treeCategory(lazy, parentId));
	}


	/**
	 * 返回树形分类集合
	 * @return 树形菜单
	 */
	@GetMapping(value = "/treeCid")
	public R getTree(Integer depth) {
		return R.ok(categoryService.treeCategoryCid(depth));
	}


	/**
     * 分页查询
     * @param page 分页对象
     * @param category 商品三级分类
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('product_category_get')" )
    public R getCategoryPage(Page page, Category category) {
        return R.ok(categoryService.page(page, Wrappers.query(category)));
    }


    /**
     * 通过id查询商品三级分类
     * @param catId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{catId}" )
    @PreAuthorize("@pms.hasPermission('product_category_get')" )
    public R getById(@PathVariable("catId" ) Long catId) {
        return R.ok(categoryService.getById(catId));
    }

    /**
     * 新增商品三级分类
     * @param category 商品三级分类
     * @return R
     */
    @ApiOperation(value = "新增商品三级分类", notes = "新增商品三级分类")
    @SysLog("新增商品三级分类" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('product_category_add')" )
    public R save(@RequestBody Category category) {
        return R.ok(categoryService.addCategory(category));
    }

    /**
     * 修改商品三级分类
     * @param category 商品三级分类
     * @return R
     */
    @ApiOperation(value = "修改商品三级分类", notes = "修改商品三级分类")
    @SysLog("修改商品三级分类" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('product_category_edit')" )
    public R updateById(@RequestBody Category category) {
        return R.ok(categoryService.updateById(category));
    }

    /**
     * 通过id删除商品三级分类
     * @param catId id
     * @return R
     */
    @ApiOperation(value = "通过id删除商品三级分类", notes = "通过id删除商品三级分类")
    @SysLog("通过id删除商品三级分类" )
    @DeleteMapping("/{catId}" )
    @PreAuthorize("@pms.hasPermission('product_category_del')" )
    public R removeById(@PathVariable Long catId) {
        return R.ok(categoryService.removeById(catId));
    }

}
